package com.jyj;

import com.jyj.comm.RedisUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.SortingParams;

import java.util.*;

/**
 * String... strs参数说明
 * 可以输入参数："str1","str2","str3"
 * 可以输入数组：new Stirng[]{"str1","str2","str3"}
 */
public class RedisTest {
    public static void main(String[] args) {

    }

    /**
     * 字符串操作
     */
    private  static void StringOperate()
    {
        Jedis jedis=getJedis();
        System.out.println("======================String_1==========================");
        // 清空数据
        System.out.println("清空库中所有数据："+jedis.flushDB());

        System.out.println("=============增=============");
        jedis.set("key001","value001");
        jedis.set("key002","value002");
        jedis.set("key003","value003");
        System.out.println("已新增的3个键值对如下：");
        System.out.println(jedis.get("key001"));
        System.out.println(jedis.get("key002"));
        System.out.println(jedis.get("key003"));

        System.out.println("=============删=============");
        System.out.println("删除key003键值对："+jedis.del("key003"));
        System.out.println("获取key003键对应的值："+jedis.get("key003"));

        System.out.println("=============改=============");
        //1、直接覆盖原来的数据
        System.out.println("直接覆盖key001原来的数据："+jedis.set("key001","value001-update"));
        System.out.println("获取key001对应的新值："+jedis.get("key001"));
        //2、直接覆盖原来的数据
        System.out.println("在key002原来值后面追加："+jedis.append("key002","+appendString"));
        System.out.println("获取key002对应的新值"+jedis.get("key002"));

        System.out.println("=============增，删，查（多个）=============");
        /**
         * mset,mget同时新增，修改，查询多个键值对
         * 等价于：
         * jedis.set("name","ssss");
         * jedis.set("jarorwar","xxxx");
         */
        System.out.println("一次性新增key201,key202,key203,key204及其对应值："+jedis.mset("key201","value201",
                "key202","value202","key203","value203","key204","value204"));
        System.out.println("一次性获取key201,key202,key203,key204各自对应的值："+
                jedis.mget("key201","key202","key203","key204"));
        System.out.println("一次性删除key201,key202："+jedis.del(new String[]{"key201", "key202"}));
        System.out.println("一次性获取key201,key202,key203,key204各自对应的值："+
                jedis.mget("key201","key202","key203","key204"));
        System.out.println();


        //jedis具备的功能shardedJedis中也可直接使用，下面测试一些前面没用过的方法
        System.out.println("======================String_2==========================");
        // 清空数据
        System.out.println("清空库中所有数据："+jedis.flushDB());

        System.out.println("=============新增键值对时防止覆盖原先值=============");
        System.out.println("原先key301不存在时，新增key301："+jedis.setnx("key301", "value301"));
        System.out.println("原先key302不存在时，新增key302："+jedis.setnx("key302", "value302"));
        System.out.println("当key302存在时，尝试新增key302："+jedis.setnx("key302", "value302_new"));
        System.out.println("获取key301对应的值："+jedis.get("key301"));
        System.out.println("获取key302对应的值："+jedis.get("key302"));

        System.out.println("=============超过有效期键值对被删除=============");
        // 设置key的有效期，并存储数据
        System.out.println("新增key303，并指定过期时间为2秒"+jedis.setex("key303", 2, "key303-2second"));
        System.out.println("获取key303对应的值："+jedis.get("key303"));
        try{
            Thread.sleep(3000);
        }
        catch (InterruptedException e){
        }
        System.out.println("3秒之后，获取key303对应的值："+jedis.get("key303"));

        System.out.println("=============获取原值，更新为新值一步完成=============");
        System.out.println("key302原值："+jedis.getSet("key302", "value302-after-getset"));
        System.out.println("key302新值："+jedis.get("key302"));

        System.out.println("=============获取子串=============");
        System.out.println("获取key302对应值中的子串："+jedis.getrange("key302", 5, 7));
    }


    private void ListOperate()
    {
     Jedis   shardedJedis=getJedis();
        System.out.println("======================list==========================");
        // 清空数据
        delAll(shardedJedis);
        System.out.println("=============增=============");
        shardedJedis.lpush("stringlists", "vector");
        shardedJedis.lpush("stringlists", "ArrayList");
        shardedJedis.lpush("stringlists", "vector");
        shardedJedis.lpush("stringlists", "vector");
        shardedJedis.lpush("stringlists", "LinkedList");
        shardedJedis.lpush("stringlists", "MapList");
        shardedJedis.lpush("stringlists", "SerialList");
        shardedJedis.lpush("stringlists", "HashList");
        shardedJedis.lpush("numberlists", "3");
        shardedJedis.lpush("numberlists", "1");
        shardedJedis.lpush("numberlists", "5");
        shardedJedis.lpush("numberlists", "2");
        System.out.println("所有元素-stringlists："+shardedJedis.lrange("stringlists", 0, -1));
        System.out.println("所有元素-numberlists："+shardedJedis.lrange("numberlists", 0, -1));

        System.out.println("=============删=============");
        // 删除列表指定的值 ，第二个参数为删除的个数（有重复时），后add进去的值先被删，类似于出栈
        System.out.println("成功删除指定元素个数-stringlists："+shardedJedis.lrem("stringlists", 2, "vector"));
        System.out.println("删除指定元素之后-stringlists："+shardedJedis.lrange("stringlists", 0, -1));
        // 删除区间以外的数据
        System.out.println("删除下标0-3区间之外的元素："+shardedJedis.ltrim("stringlists", 0, 3));
        System.out.println("删除指定区间之外元素后-stringlists："+shardedJedis.lrange("stringlists", 0, -1));
        // 列表元素出栈
        System.out.println("出栈元素："+shardedJedis.lpop("stringlists"));
        System.out.println("元素出栈后-stringlists："+shardedJedis.lrange("stringlists", 0, -1));

        System.out.println("=============改=============");
        // 修改列表中指定下标的值
        shardedJedis.lset("stringlists", 0, "hello list!");
        System.out.println("下标为0的值修改后-stringlists："+shardedJedis.lrange("stringlists", 0, -1));
        System.out.println("=============查=============");
        // 数组长度
        System.out.println("长度-stringlists："+shardedJedis.llen("stringlists"));
        System.out.println("长度-numberlists："+shardedJedis.llen("numberlists"));
        // 排序
        /*
         * list中存字符串时必须指定参数为alpha，如果不使用SortingParams，而是直接使用sort("list")，
         * 会出现"ERR One or more scores can't be converted into double"
         */
        SortingParams sortingParameters = new SortingParams();
        sortingParameters.alpha();
        sortingParameters.limit(0, 3);
        System.out.println("返回排序后的结果-stringlists："+shardedJedis.sort("stringlists",sortingParameters));
        System.out.println("返回排序后的结果-numberlists："+shardedJedis.sort("numberlists"));
        // 子串：  start为元素下标，end也为元素下标；-1代表倒数一个元素，-2代表倒数第二个元素
        System.out.println("子串-第二个开始到结束："+shardedJedis.lrange("stringlists", 1, -1));
        // 获取列表指定下标的值
        System.out.println("获取下标为2的元素："+shardedJedis.lindex("stringlists", 2)+"\n");
    }

    public static void addList(Jedis jedis,String key,String[] cont){
        //存储数据到列表中
        jedis.lpush(key,cont);

    }

    /**
     * 新增字符串key,无则新增，有则覆盖
     * @param jedis
     * @param key
     * @param cont
     */
    public static  void addStr(Jedis jedis,String key,String cont){

        jedis.set(key,cont);
    }

    /**
     * 设置过期时间
     * @param jedis
     * @param key
     * @param cont
     * @param timeout
     */
    public static void addStrTimeout(Jedis jedis,String key,String cont,Integer timeout){
        System.out.println(jedis.setex(key,timeout,cont));
    }


    /**
     * 新增多个，
     * @param jedis
     * @param keysvalues "key1","kval1","key2","keyval2" ..
     */
    public static  void addStrs(Jedis jedis,String... keysvalues){
        jedis.mset(keysvalues);
    }

    /**
     * 得到一个内容，如果key不存在，则返回null
     * @param jedis
     * @param key
     * @return
     */
    public static String getStr(Jedis jedis,String key){

        return jedis.get(key);
    }

    /**
     * 得到多个key值
     * @param jedis
     * @param keys eg: "k1","k2"
     * @return
     */
    public static List<String> getStrs(Jedis jedis,String... keys){
        return jedis.mget(keys);
    }

    /**
     * 增加list数据类型，注意，不会去重
     * @param jedis
     * @param key
     * @param values
     */
    public static void setList(Jedis jedis,String key,String... values){
        jedis.lpush(key,values);
    }

    /**
     * 得到所有list数据
     * @param jedis
     * @param key
     * @return
     */
    public static List<String> getListAll(Jedis jedis,String key){
       return  jedis.lrange(key,0,-1) ;
    }

    /**
     * 得到list
     * @param jedis
     * @param key
     * @param start 开始索引
     * @param end  结束索引 -1表示到最后一个索引
     * @return
     */
    public static  List<String> getList(Jedis jedis,String key,int start,int end){
        List<String> list = jedis.lrange(key, start ,end);
        return  list;
    }
    /**
     * 删除一个key
     * @param jedis
     * @param key
     */

    public static void delKey(Jedis jedis,String key) {
        if(jedis.exists(key)){

            jedis.del(key);
            System.out.println("删除成功,key="+key);
        }
    }

    /**
     * 设置过期时间
     * @param jedis
     * @param key
     * @param seconds
     */
    public static void setExpireTime(Jedis jedis,String key,int seconds){
        jedis.expire(key, seconds);
    }
    /**
     * 删除多个key
     * @param jedis
     * @param keys  eg:"k1","k2"
     */
    public static void delKeys(Jedis jedis,String... keys){
        jedis.del(keys);

    }

    /**
     * 删除所有key
     * @param jedis
     */
    public static void delAll(Jedis jedis){
        System.out.println("清空库中所有数据："+jedis.flushDB());
    }

    /**
     * 得到所有key
     * @param jedis
     * @return
     */
    public  static  Set<String>  getAllKey(Jedis jedis){
        // 获取数据并输出
        Set<String> keys = jedis.keys("*");
        return keys;
    }


    /**
     * 链接jdis
     * @return
     */
    private  static  Jedis getJedis(){
        //连接本地的 Redis 服务
        Jedis jedis = new Jedis("localhost",6379,2000);
     //   jedis.auth("segns");//设置密码
        System.out.println("连接成功");
        return jedis;
    }


}
